/*
 * @Description:
 * @Version:
 * @Author: Troubadour
 * @Date: 2023-01-16 22:02:43
 * @LastEditors: Troubadour 2276791354@qq.com
 * @LastEditTime: 2023-11-05 00:50:05
 */

#include "tft_1_3.h"

/**
 * @function: TFT_Write_Data
 * @brief 写8bit数据
 * @param {*}
 * @return {*}
 */
void TFT_Write_Data(uint8_t data)
{
#if (TFT_HWSPI_ENABLE)
    HAL_SPI_Transmit(&hspi1, &data, 1, 10);
#else

#endif /* TFT_HWSPI_ENABLE */
}

/**
 * @function: TFT_Write_Buff
 * @brief 写入一定长度数据
 * @param {*}
 * @return {*}
 */
void TFT_Write_Buff(uint8_t data, uint16_t len)
{
#if (TFT_HWSPI_ENABLE)
    HAL_SPI_Transmit(&hspi1, &data, len, 100);
#else

#endif /* TFT_HWSPI_ENABLE */
}

/**
 * @function: TFT_Write_Data8
 * @brief 向TFT写入8Bit数据
 * @param {*}
 * @return {*}
 */
void TFT_Write_Data8(uint8_t data)
{
    TFT_DC(1);
    TFT_Write_Data(data);
}

/**
 * @function: TFT_Write_Data16
 * @brief 向TFT写入16Bit数据
 * @param {*}
 * @return {*}
 */
void TFT_Write_Data16(uint16_t data)
{
    TFT_DC(1);
    TFT_Write_Data(data >> 8);
    TFT_Write_Data(data);
}

/**
 * @function: TFT_Write_REG
 * @brief 向TFT寄存器写入8Bit命令
 * @param {*}
 * @return {*}
 */
void TFT_Write_REG(uint8_t cmd)
{
    TFT_DC(0);
    TFT_Write_Data(cmd);
}

/**
 * @function: TFT_Write_REG_DATA
 * @brief 向一个寄存器写入一个数据
 * @param {uint8_t} reg：寄存器
 * @param {uint8_t} data：数据
 * @return {*}
 */
void TFT_Write_REG_DATA(uint8_t reg, uint8_t data)
{
    TFT_Write_REG(reg);
    TFT_Write_Data(data);
}

/**
 * @function: TFT_Address_set
 * @brief 地址设置
 * @param {uint32_t} x1：起始地址x
 * @param {uint32_t} y1：起始地址y
 * @param {uint32_t} x2：结束地址x
 * @param {uint32_t} y2：结束地址y
 * @return {*}
 */
void TFT_Address_set(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2)
{
    TFT_Write_REG(0x2A);
    TFT_Write_Data8(x1 >> 8);
    TFT_Write_Data8(x1);
    TFT_Write_Data8(x2 >> 8);
    TFT_Write_Data8(x2);

    TFT_Write_REG(0x2B);
    TFT_Write_Data8(y1 >> 8);
    TFT_Write_Data8(y1);
    TFT_Write_Data8(y2 >> 8);
    TFT_Write_Data8(y2);

    TFT_Write_REG(0x2C);
}

/**
 * @function: TFT_Init
 * @brief 初始化
 * @return {*}
 */
void TFT_Init(void)
{
    TFT_RES(0);
    HAL_Delay(50);
    TFT_RES(1);
    HAL_Delay(20);
    TFT_BLK(1);

    //************* Start Initial Sequence **********//
    TFT_Write_REG(0x11);
    TFT_Write_REG(0x36);
    TFT_Write_Data8(0x00); // 0x00-正常， 0x40-翻转（适用分光棱镜）

    TFT_Write_REG(0x3A);
    TFT_Write_Data8(0x05);

    TFT_Write_REG(0xB2);
    TFT_Write_Data8(0x0C);
    TFT_Write_Data8(0x0C);
    TFT_Write_Data8(0x00);
    TFT_Write_Data8(0x33);
    TFT_Write_Data8(0x33);

    TFT_Write_REG(0xB7);
    TFT_Write_Data8(0x35);

    TFT_Write_REG(0xBB);
    TFT_Write_Data8(0x19);

    TFT_Write_REG(0xC0);
    TFT_Write_Data8(0x2C);

    TFT_Write_REG(0xC2);
    TFT_Write_Data8(0x01);

    TFT_Write_REG(0xC3);
    TFT_Write_Data8(0x12);

    TFT_Write_REG(0xC4);
    TFT_Write_Data8(0x20);

    TFT_Write_REG(0xC6);
    TFT_Write_Data8(0x0F);

    TFT_Write_REG(0xD0);
    TFT_Write_Data8(0xA4);
    TFT_Write_Data8(0xA1);

    TFT_Write_REG(0xE0);
    TFT_Write_Data8(0xD0);
    TFT_Write_Data8(0x04);
    TFT_Write_Data8(0x0D);
    TFT_Write_Data8(0x11);
    TFT_Write_Data8(0x13);
    TFT_Write_Data8(0x2B);
    TFT_Write_Data8(0x3F);
    TFT_Write_Data8(0x54);
    TFT_Write_Data8(0x4C);
    TFT_Write_Data8(0x18);
    TFT_Write_Data8(0x0D);
    TFT_Write_Data8(0x0B);
    TFT_Write_Data8(0x1F);
    TFT_Write_Data8(0x23);

    TFT_Write_REG(0xE1);
    TFT_Write_Data8(0xD0);
    TFT_Write_Data8(0x04);
    TFT_Write_Data8(0x0C);
    TFT_Write_Data8(0x11);
    TFT_Write_Data8(0x13);
    TFT_Write_Data8(0x2C);
    TFT_Write_Data8(0x3F);
    TFT_Write_Data8(0x44);
    TFT_Write_Data8(0x51);
    TFT_Write_Data8(0x2F);
    TFT_Write_Data8(0x1F);
    TFT_Write_Data8(0x1F);
    TFT_Write_Data8(0x20);
    TFT_Write_Data8(0x23);

    TFT_Write_REG(0x21);

    TFT_Write_REG(0x11);
    // HAL_Delay(120);
    TFT_Write_REG(0x29);
}

void TFT_ST7796_Init(void)
{
    HAL_Delay(120);

    TFT_Write_REG(0x01); // Software reset
    HAL_Delay(120);

    TFT_Write_REG(0x11); // Sleep exit
    HAL_Delay(120);

    TFT_Write_REG(0xF0); // Command Set control
    TFT_Write_Data8(0xC3);    // Enable extension command 2 partI

    TFT_Write_REG(0xF0); // Command Set control
    TFT_Write_Data8(0x96);    // Enable extension command 2 partII

    TFT_Write_REG(0x36); // Memory Data Access Control MX, MY, RGB mode
    TFT_Write_Data8(0x48);    // X-Mirror, Top-Left to right-Buttom, RGB

    TFT_Write_REG(0x3A); // Interface Pixel Format
    TFT_Write_Data8(0x55);    // Control interface color format set to 16

    TFT_Write_REG(0xB4); // Column inversion
    TFT_Write_Data8(0x01);    // 1-dot inversion

    TFT_Write_REG(0xB6); // Display Function Control
    TFT_Write_Data8(0x80);    // Bypass
    TFT_Write_Data8(0x02);    // Source Output Scan from S1 to S960, Gate Output scan from G1 to G480, scan cycle=2
    TFT_Write_Data8(0x3B);    // LCD Drive Line=8*(59+1)

    TFT_Write_REG(0xE8); // Display Output Ctrl Adjust
    TFT_Write_Data8(0x40);
    TFT_Write_Data8(0x8A);
    TFT_Write_Data8(0x00);
    TFT_Write_Data8(0x00);
    TFT_Write_Data8(0x29); // Source eqaulizing period time= 22.5 us
    TFT_Write_Data8(0x19); // Timing for "Gate start"=25 (Tclk)
    TFT_Write_Data8(0xA5); // Timing for "Gate End"=37 (Tclk), Gate driver EQ function ON
    TFT_Write_Data8(0x33);

    TFT_Write_REG(0xC1); // Power control2
    TFT_Write_Data8(0x06);    // VAP(GVDD)=3.85+( vcom+vcom offset), VAN(GVCL)=-3.85+( vcom+vcom offset)

    TFT_Write_REG(0xC2); // Power control 3
    TFT_Write_Data8(0xA7);    // Source driving current level=low, Gamma driving current level=High

    TFT_Write_REG(0xC5); // VCOM Control
    TFT_Write_Data8(0x18);    // VCOM=0.9

    HAL_Delay(120);

    // ST7796 Gamma Sequence
    TFT_Write_REG(0xE0); // Gamma"+"
    TFT_Write_Data8(0xF0);
    TFT_Write_Data8(0x09);
    TFT_Write_Data8(0x0b);
    TFT_Write_Data8(0x06);
    TFT_Write_Data8(0x04);
    TFT_Write_Data8(0x15);
    TFT_Write_Data8(0x2F);
    TFT_Write_Data8(0x54);
    TFT_Write_Data8(0x42);
    TFT_Write_Data8(0x3C);
    TFT_Write_Data8(0x17);
    TFT_Write_Data8(0x14);
    TFT_Write_Data8(0x18);
    TFT_Write_Data8(0x1B);

    TFT_Write_REG(0xE1); // Gamma"-"
    TFT_Write_Data8(0xE0);
    TFT_Write_Data8(0x09);
    TFT_Write_Data8(0x0B);
    TFT_Write_Data8(0x06);
    TFT_Write_Data8(0x04);
    TFT_Write_Data8(0x03);
    TFT_Write_Data8(0x2B);
    TFT_Write_Data8(0x43);
    TFT_Write_Data8(0x42);
    TFT_Write_Data8(0x3B);
    TFT_Write_Data8(0x16);
    TFT_Write_Data8(0x14);
    TFT_Write_Data8(0x17);
    TFT_Write_Data8(0x1B);

    HAL_Delay(120);

    TFT_Write_REG(0xF0); // Command Set control
    TFT_Write_Data8(0x3C);    // Disable extension command 2 partI

    TFT_Write_REG(0xF0); // Command Set control
    TFT_Write_Data8(0x69);    // Disable extension command 2 partII

    HAL_Delay(120);

    TFT_Write_REG(0x29); // Display on
}
